library IEEE;
use IEEE.std_logic_1164.all;
use work.state_pkg.all;

entity marsrover is
  port (
    clk : in std_logic;
    
    sensor_l : in std_logic;
    sensor_m : in std_logic;
    sensor_r : in std_logic;
    reset : in std_logic;
    
    motor_l : out std_logic; 
    motor_r : out std_logic;
	 
	 
		display_data	: out	std_logic_vector (7 downto 0);
		display_enable	: out	std_logic_vector (3 downto 0)
    
  );
end marsrover;

architecture structural of marsrover is
  component input_buffer 
	port (	clk		: in	std_logic;

		sensor_l_in	: in	std_logic;
		sensor_m_in	: in	std_logic;
		sensor_r_in	: in	std_logic;

		sensor_l_out	: out	std_logic;
		sensor_m_out	: out	std_logic;
		sensor_r_out	: out	std_logic
	);
  end component;
  component controller2 
	port (	clk			: in	std_logic;
	  
		reset			: in	std_logic;

		sensor_l		: in	std_logic;
		sensor_m		: in	std_logic;
		sensor_r		: in	std_logic;

		count_in		: in	std_logic_vector (19 downto 0);
		count_reset		: out	std_logic;

		rover_direction : out direction_type;
		

		display_data	: out	std_logic_vector (7 downto 0);
		display_enable	: out	std_logic_vector (3 downto 0)
  );
  end component;
  
  component motor_control
  port ( 
      rover_direction : in direction_type;
      
		  motor_l_reset		: out	std_logic;
		  motor_l_direction	: out	std_logic;

		  motor_r_reset		: out	std_logic;
		  motor_r_direction	: out	std_logic
	);
	end component;
  component counter 
	port (	clk		: in	std_logic;
		reset		: in	std_logic;

		count_out	: out	std_logic_vector (19 downto 0)
	);
  end component;
  component pwm_generator 
	port (	clk		: in	std_logic;
		reset		: in	std_logic;
		direction	: in	std_logic;
		count_in	: in	std_logic_vector (19 downto 0);
		
		

		pwm		: out	std_logic
	);
  end component;
signal sensor_l_buf, sensor_r_buf, sensor_m_buf : std_logic;
signal count : std_logic_vector(19 downto 0);
signal counter_reset : std_logic;
signal direction_lw, reset_lw : std_logic;
signal direction_rw, reset_rw : std_logic;
signal rover_direction : direction_type;
  
begin
  lbl_ibuf: input_buffer PORT MAP (clk, 
              sensor_l, sensor_m, sensor_r, 
              sensor_l_buf, sensor_r_buf, sensor_m_buf);
				  
  lbl_ctrl: controller2 PORT MAP (clk => clk, 
              sensor_l => sensor_l_buf, 
              sensor_r => sensor_r_buf, 
              sensor_m => sensor_m_buf,
              reset => reset,
              count_in =>count, 
              count_reset => counter_reset,
              rover_direction => rover_direction,
				  display_data	=> display_data,
				  display_enable => display_enable);
				  
  lbl_mctrl: motor_control PORT MAP( 
              rover_direction => rover_direction,
              motor_l_direction => direction_lw,
              motor_l_reset => reset_lw,
              motor_r_direction => direction_rw,
              motor_r_reset => reset_rw);
    
  lbl_count: counter PORT MAP (clk, counter_reset, count);
  lbl_pwml: pwm_generator PORT MAP ( clk, 
              reset_lw, direction_lw, count, motor_l);
  lbl_pwmr: pwm_generator PORT MAP ( clk, 
              reset_rw, direction_rw, count, motor_r);
end structural;

